package com.vote.www.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.vote.www.domain.Czjact;
import com.vote.www.domain.Czjbag;
import com.vote.www.domain.Czjcopyact;
import com.vote.www.domain.Czjcopybag;
import com.vote.www.domain.Czjorder;
import com.vote.www.domain.Czjuserinfo;
import com.vote.www.service.CzjactService;
import com.vote.www.service.CzjbagService;
import com.vote.www.service.CzjcopyactService;
import com.vote.www.service.CzjcopybagService;
import com.vote.www.service.CzjorderService;
import com.vote.www.service.CzjuserinfoService;
import com.vote.www.utils.OrderNumUtils;
import com.vote.www.utils.UUIDUtils;

@Controller
@RequestMapping(value = "/order")
public class CzjorderController extends BaseController {
	private static final Logger log = LoggerFactory
			.getLogger(CzjorderController.class);

	public CzjorderController() {
	}

	/**
	 * 订单表（czj_order）保存
	 * 
	 * @param czjorder
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjorderSave")
	public void save(Czjorder czjorder,String commodityIds, String nums,
			HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = true) String t, ModelMap model)
			throws Exception {

		try {
			if (StringUtils.isBlank(czjorder.getUserInfoId())) {
				jsonResult(0, "117", "用户为空", "", response);
				return;
			}
			
			Map<String, Object> userMap=new HashMap<String, Object>();
				userMap.put("id", czjorder.getUserInfoId());
			Czjuserinfo czjuserinfo=czjuserinfoService.getCzjuserinfo(userMap);
			if(null==czjuserinfo || "".equals(czjuserinfo)){
				 jsonResult(0, "112", "用户不存在", "", response);
				 return ;
			}

			List<Czjorder> czjorders = new ArrayList<Czjorder>();
			List<Czjcopyact> czjcopyacts = new ArrayList<Czjcopyact>();
		/*	List<Czjcopybag> czjcopybags = new ArrayList<Czjcopybag>();*/
			
			
			String [] comm=commodityIds.split(",");
			String [] num=nums.split(",");
			if(comm.length<0){
				 jsonResult(0, "118", "奖品错误", "", response);
				 return ;
			}
			List<String> ids=new ArrayList<String>();
			for(String str:comm){
				ids.add(str);
			}
			List<String> totals=new ArrayList<String>();
			for(String str:num){
				totals.add(str);
			}
			Czjorder c = null;
			if (ids != null && ids.size() > 0) {
				for (int i = 0; i < ids.size(); i++) {
					c = new Czjorder();
					c.setType(czjorder.getType());
					c.setUserInfoId(czjorder.getUserInfoId());
					if (StringUtils.isNotBlank(czjorder.getRemards())) {

						c.setRemards(czjorder.getRemards());
					}
					if (StringUtils.isNotBlank(czjorder.getConsignName())) {

						c.setConsignName(czjorder.getConsignName());
					}
					if (StringUtils.isNotBlank(czjorder.getDeliveryAddress())) {

						c.setDeliveryAddress(czjorder.getDeliveryAddress());
					}
					c.setCommodityId(ids.get(i));
					c.setNum(Integer.valueOf(totals.get(i)));
					c.setIsDel(1);// 1未删除
					c.setIsShow(1);// 显示
					c.setStatus(2);// 1待支付 2待发货 3已完成
					c.setCreatetime(new Date());
					c.setOrderNum(OrderNumUtils.getOrderNo(1));
					c.setOrderId(UUIDUtils.getUUID());
					czjorders.add(c);
					Map<String, Object> actMap = null;
					if (t.equals("1")) { //代表活动
						actMap = new HashMap<String, Object>();
						Czjact act = new Czjact();
						actMap.put("actId", ids.get(i));
						act = czjactService.getCzjact(actMap);
						Czjcopyact czjcopyact = new Czjcopyact();

						czjcopyact.setCzjcopyactId(UUIDUtils.getUUID());
						czjcopyact.setOrderId(c.getOrderId());

						if (StringUtils.isNotBlank(act.getActId())) {

							czjcopyact.setActId(act.getActId());
						}
						if (StringUtils.isNotBlank(act.getImgPath())) {

							czjcopyact.setImgPath(act.getImgPath());
						}
						if (StringUtils.isNotBlank(act.getImgUrl())) {

							czjcopyact.setImgUrl(act.getImgUrl());
						}
						if (StringUtils.isNotBlank(act.getRemark())) {

							czjcopyact.setRemark(act.getRemark());
						}
						czjcopyact.setCreatetime(new Date());

						if (StringUtils.isNotBlank(act.getExt1())) {
							czjcopyact.setExt1(act.getExt1());
						}

						if (StringUtils.isNotBlank(act.getExt2())) {
							czjcopyact.setExt2(act.getExt2());
						}
						if (StringUtils.isNotBlank(act.getExt3())) {
							czjcopyact.setExt3(act.getExt3());
						}
						czjcopyacts.add(czjcopyact);
					}
					if (t.equals("2")) {//2 代表商品

						actMap = new HashMap<String, Object>();
						Czjbag czjbag = new Czjbag();
						actMap.put("bagId", ids.get(i));
						czjbag = czjbagService.getCzjbag(actMap);
						Czjcopyact czjcopyact = new Czjcopyact();

						czjcopyact.setCzjcopyactId(UUIDUtils.getUUID());
						czjcopyact.setOrderId(c.getOrderId());
						if (StringUtils.isNotBlank(czjbag.getBagId())) {
							czjcopyact.setActId(czjbag.getBagId());

						}
						if (StringUtils.isNotBlank(czjbag.getImgPath())) {
							czjcopyact.setImgPath(czjbag.getImgPath());

						}
						if (StringUtils.isNotBlank(czjbag.getImgUrl())) {
							czjcopyact.setImgUrl(czjbag.getImgUrl());

						}
						if (StringUtils.isNotBlank(czjbag.getRemark())) {

							czjcopyact.setRemark(czjbag.getRemark());
						}

						czjcopyact.setCreatetime(new Date());
						if (StringUtils.isNotBlank(czjbag.getExt1())) {

							czjcopyact.setExt1(czjbag.getExt1());
						}
						if (StringUtils.isNotBlank(czjbag.getExt2())) {

							czjcopyact.setExt2(czjbag.getExt2());
						}
						if (StringUtils.isNotBlank(czjbag.getExt3())) {

							czjcopyact.setExt3(czjbag.getExt3());
						}
						czjcopyacts.add(czjcopyact);
					}

				}
			}
			for (Czjorder list : czjorders) {
				czjorderService.insertCzjorder(list);
			}
		/*	for (Czjcopybag list : czjcopybags) {
				czjcopybagService.insertCzjcopybag(list);
			}*/
			for (Czjcopyact list : czjcopyacts) {
				czjcopyactService.insertCzjcopyact(list);
			}
			jsonResult(1, "", "预定礼品成功", "", response);

		} catch (Exception e) {
			e.printStackTrace();
			log.info("error", e);
			jsonResult(0, "116", "礼品预定失败", "", response);
		}

	}

	@Resource
	private CzjorderService czjorderService;

	@Resource
	private CzjactService czjactService;

	@Resource
	private CzjcopyactService czjcopyactService;

	@Resource
	private CzjbagService czjbagService;

	@Resource
	private CzjcopybagService czjcopybagService;
	
	@Resource
	private CzjuserinfoService czjuserinfoService;

}